package com.dushuren.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dushuren.project.common.ErrorCode;
import com.dushuren.project.exception.BusinessException;
import com.dushuren.project.mapper.SuggestNotWordsSearchMapper;
import com.dushuren.project.model.dto.SearchWordsRequest;
import com.dushuren.project.model.entity.SuggestNotWordsSearch;
import com.dushuren.project.model.entity.SuggestWordsSearch;
import com.dushuren.project.model.vo.SuggestWordsSearchVo;
import com.dushuren.project.service.SuggestNotWordsSearchService;
import com.dushuren.project.service.SuggestWordsSearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author qieqie
* @description 针对表【suggest_not_words_search(搜索记录表)】的数据库操作Service实现
* @createDate 2022-11-02 09:19:45
*/
@Service
public class SuggestNotWordsSearchServiceImpl extends ServiceImpl<SuggestNotWordsSearchMapper, SuggestNotWordsSearch>
    implements SuggestNotWordsSearchService{

  @Resource
  SuggestNotWordsSearchMapper suggestNotWordsSearchMapper;

  @Resource
  SuggestWordsSearchService suggestWordsSearchService;

  /**
   * 校验 Words 中是否存在 违规词
   * @param searchInformation
   * @return
   */
  @Override
  public boolean wordsIsDeregulation(String searchInformation) {
	// todo 修改为查询 缓存 减少 数据库的线程访问
	QueryWrapper<SuggestNotWordsSearch> queryWrapper = new QueryWrapper<>();
	queryWrapper.select("id");
	for (int i = 0; i < searchInformation.length(); i++) {
	  queryWrapper.like("searchInformation",searchInformation.indexOf(i)).or();
	}
	Long count = suggestNotWordsSearchMapper.selectCount(queryWrapper);

	return count == 0;
  }

  @Override
  public Page<SuggestWordsSearchVo> queryListData(SearchWordsRequest searchWordsRequest) {
	String searchInformation = searchWordsRequest.getSearchInformation();
	long current = searchWordsRequest.getCurrent();
	long pageSize = searchWordsRequest.getPageSize();

	QueryWrapper<SuggestNotWordsSearch> queryWrapper = new QueryWrapper<>();
	queryWrapper.select("id", "searchInformation", "searchNumber", "createTime");
	queryWrapper.like(StringUtils.isNotBlank(searchInformation), "searchInformation", searchInformation);

	Page<SuggestNotWordsSearch> page = this.page(new Page<>(current, pageSize), queryWrapper);
	List<SuggestWordsSearchVo> suggestWordsNotSearchVo = this.getSuggestWordsNotSearchVo(page);

	Page<SuggestWordsSearchVo> pageSearch = new Page<>(page.getSize() , page.getCurrent(), page.getTotal());
	pageSearch.setRecords(suggestWordsNotSearchVo);
	return pageSearch;
  }

  @Override
  public List<SuggestWordsSearchVo> getSuggestWordsNotSearchVo(Page<SuggestNotWordsSearch> page) {
	return page.getRecords().stream().map(suggestWordsSearch -> {
	  SuggestWordsSearchVo suggestWordsSearchVo = new SuggestWordsSearchVo();
	  suggestWordsSearchVo.setId(suggestWordsSearch.getId());
	  suggestWordsSearchVo.setSearchCategory(1L);
	  suggestWordsSearchVo.setSearchInformation(suggestWordsSearch.getSearchInformation());
	  suggestWordsSearchVo.setSearchNumber(suggestWordsSearch.getSearchNumber());
	  suggestWordsSearchVo.setCreateTime(suggestWordsSearch.getCreateTime());
	  return suggestWordsSearchVo;
	}).collect(Collectors.toList());
  }

  @Override
  public Long deleteSearchWords(Long searchId) {
	try {
	  this.removeById(searchId);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return 0L;
  }

  @Override
  public Long updateSearchWords(SuggestNotWordsSearch suggestNotWordsSearch) {
	try {
	  this.updateById(suggestNotWordsSearch);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return 0L;
  }

  @Override
  public Long addSearchWords(SuggestNotWordsSearch suggestNotWordsSearch) {
	try {
	  this.save(suggestNotWordsSearch);
	} catch (Exception e) {
	  throw new BusinessException(ErrorCode.SYSTEM_ERROR, "传入值异常");
	}
	return null;
  }
}




